//
//  ProjectListViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/27.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class ProjectListViewController: RefreshedTableViewController,ProjectSearch {
    
//    var searchmatters: Bool {
//        get{
//            return self.orgCode.count > 0 || (self.searchsHeader.yearField.text?.count ?? 0) > 0 || (self.searchsHeader.projectNameField.text?.count ?? 0) > 0
//        }
//    }
    
    
    override func param(for name: String) -> Dictionary<String,Any>{
        
//        if searchmatters {
//            return ["orgCode":self.orgCode,"gsProjName": self.searchsHeader.yearField.text ?? "","projManagerName": self.searchsHeader.projectNameField.text ?? ""] + ["curPage": "1","pageSize": "10000000"]
//        }
        
        return ["orgCode":self.orgCode,"gsProjName": self.searchsHeader.yearField.text ?? "","projManagerName": self.searchsHeader.projectNameField.text ?? ""] + super.param(for: name)
    }
    
    override func path(for name: String) -> String{
        return GetProjectList
    }
    
    override func orgblock()->Void{
        self.orgCode = ""
//        let items = self.companys.filter { (json) -> Bool in
//            return json.contents?["code"]?.rawString() == self.orgCode
//        }
//        if items.count > 0 {
//            self.searchsHeader.orgField.text = items[0].contents?["name"]?.rawString()
//        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAction))
        searchButton.tintColor = UIColor.white
        self.navigationItem.rightBarButtonItem = searchButton
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.searchsHeader.delegate = self
        self.searchsHeader.yearField.placeholder = "项目名称未选择"
        self.searchsHeader.projectNameField.placeholder = "项目经理未选择"
        self.tableView.register(UINib(nibName: "ProjectListCell", bundle: Bundle.main), forCellReuseIdentifier: "ProjectListCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func search(with orgcode: String?, projectname: String?, projectmanagername: String?) {
        let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAction))
        self.navigationItem.rightBarButtonItem = searchButton
        if (orgcode?.count ?? 0) > 0 {
            self.orgCode = orgcode!
            let targetitem = self.companys.filter { (json) -> Bool in
                return json.contents?["code"]?.rawString() == self.orgCode
            }
            if targetitem.count > 0 {
                let item = targetitem[0]
                self.searchsHeader.orgField.text = item.contents?["name"]?.rawString()
            }
        }
        
        self.searchsHeader.yearField.text = projectname
        
        self.searchsHeader.projectNameField.text = projectmanagername
        
        self.tableView.reloadData()
        
        self.page = 1
        
        self.load(with: "refresh")
    }
    
    private var _neworgCode: String = ""
    
    override var orgCode: String {
        set{
            _neworgCode = newValue
        }
        get{
            return _neworgCode
        }
    }
    
    override func reset() {
        let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAction))
        self.navigationItem.rightBarButtonItem = searchButton
        self.orgCode = ""
        self.searchsHeader.yearField.text = nil
        self.searchsHeader.projectNameField.text = nil
        self.page = 1
        self.tableView.reloadData()
        //加载数据
    }
    
    let searchsHeader = Bundle.main.loadNibNamed("ProjectConditionHeader", owner:nil, options: nil)?[0] as! ProjectConditionHeader
    
    @objc override func searchAction()->Void{
        if self.presentedViewController == nil {
            let button = UIBarButtonItem.init(title: "取消", style: .plain, target: self, action: #selector(searchAction))
            button.tintColor = UIColor.white
            self.navigationItem.rightBarButtonItem = button
            let search = ProjectSearchViewController()
            search.companys = self.companys
            search.delegate = self
            search.selectedOrg = self.orgCode
            if (self.searchsHeader.yearField.text?.count ?? 0) > 0 {
                search.projname = self.searchsHeader.yearField.text ?? ""
            }
            if (self.searchsHeader.projectNameField.text?.count ?? 0) > 0 {
                search.projmanager = self.searchsHeader.projectNameField.text ?? ""
            }
            self.present(vc: search)
        } else {
            let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAction))
            self.navigationItem.rightBarButtonItem = searchButton
            self.presentedViewController?.dismiss()
        }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.source.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        return tableView.dequeueReusableCell(withIdentifier: "ProjectListCell")!
        
    }
    
    var showheader: Bool{
        get{
            let orgresult = self.orgCode.count > 0
            let projname = (self.searchsHeader.yearField.text?.count ?? 0) > 0
            let projmanager = (self.searchsHeader.projectNameField.text?.count ?? 0) > 0
            return orgresult || projname || projmanager
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if showheader == true {
            if section == 0 {
                let line = UIView()
                line.backgroundColor = UIColor.hex("eeeeee")
                searchsHeader.addSubview(line)
                line.snp.makeConstraints { (make) in
                    make.bottom.equalToSuperview()
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(1)
                }
                return searchsHeader
            }
        }
        return nil
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if showheader == true {
            if section == 0 {
                return 40
            }
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 60
        
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let json = self.source[indexPath.section]
        
        let cel = cell as! ProjectListCell
        
        cel.model = json
        //扶植
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: false)
        
        let json = self.source[indexPath.section]
        
        let projectDetail = CreateProjectViewController()
        
        projectDetail.vcCount = 5
        
        projectDetail.applyid = (json.contents?["id"]?.rawString()) ?? ""
        
        projectDetail.projType = "2"
        
        projectDetail.title = "项目详情"
        
        self.push(destination: projectDetail)
        
    }

}
